package com.report.component;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.SearchResult;
import cn.zhxu.bs.operator.Equal;
import cn.zhxu.bs.util.MapBuilder;
import cn.zhxu.bs.util.MapUtils;
import com.report.dto.request.WebMemberCollectReq;
import com.report.dto.response.WebMemberCollectRes;
import com.report.dto.response.WebMemberDataImageRes;
import com.report.entity.Collect;
import com.report.service.ICollectService;
import com.zmh.framework.exception.ResultException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author summer
 * @date 2024-06-28 23:32
 */
@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class WebMemberCollectComponent {

    private final BeanSearcher beanSearcher;

    private final ICollectService collectService;

    public Page<WebMemberCollectRes> getWebMemberCollectRes(WebMemberCollectReq webMemberCollectReq) {
        Page<WebMemberCollectRes>  page=new Page<>(webMemberCollectReq.getPageNum(), webMemberCollectReq.getPageSize());
        MapBuilder op = MapUtils.builder().page(webMemberCollectReq.getPageNum() - 1, webMemberCollectReq.getPageSize())
                .field(WebMemberCollectRes::getMemberId, webMemberCollectReq.getMemberId()).op(Equal.class)
                .field(WebMemberCollectRes::getSeparate, webMemberCollectReq.getSeparate()).op(Equal.class);
        SearchResult<WebMemberCollectRes> search = beanSearcher.search(WebMemberCollectRes.class, op.build());
        page.setTotal(search.getTotalCount().longValue());
        page.setRecords(search.getDataList());
        return page;
    }

    public void saveWebMemberCollect(WebMemberCollectReq webMemberCollectReq) {
        Object loginId = StpUtil.getLoginId();
        Collect collect = Convert.convert(Collect.class, webMemberCollectReq);
        collect.setMemberId(Convert.convert(Integer.class, loginId));
        LambdaQueryWrapper<Collect> eq = Wrappers.lambdaQuery(Collect.class).eq(Collect::getReportId, webMemberCollectReq.getReportId())
                .eq(Collect::getMemberId, loginId);
        Collect one = collectService.getOne(eq);
        if(ObjectUtil.isNotEmpty(one)){
            throw new ResultException("该报告已收藏");
        }
        collectService.save(collect);
    }

    public void cancelWebMemberBrowse(Integer browseId) {
        collectService.removeById(browseId);
    }



    public Page<WebMemberDataImageRes> collectImageData(WebMemberCollectReq webMemberCollectReq) {
        Page<WebMemberDataImageRes>  page=new Page<>(webMemberCollectReq.getPageNum(), webMemberCollectReq.getPageSize());
        MapBuilder op = MapUtils.builder().page(webMemberCollectReq.getPageNum() - 1, webMemberCollectReq.getPageSize())
                .field(WebMemberDataImageRes::getMemberId, webMemberCollectReq.getMemberId()).op(Equal.class)
                .field(WebMemberDataImageRes::getSeparate, webMemberCollectReq.getSeparate()).op(Equal.class);
        SearchResult<WebMemberDataImageRes> search = beanSearcher.search(WebMemberDataImageRes.class, op.build());
        page.setTotal(search.getTotalCount().longValue());
        page.setRecords(search.getDataList());
        return page;
    }
}
